//
//  AppDelegate.m
//  ent
//
//  Created by fanwu on 15/4/29.
//  Copyright (c) 2015年 ND. All rights reserved.
//

#import "AppDelegate.h"
#import <UCSDK/UCSDK.h>
#import <MUPFoundation/MUPFoundation.h>
#import <SmartCan/SmartCan.h>
#import <UMengAnalytics-NO-IDFA/MobClick.h>
#import "SetupUIViewController.h"
#import "FWLoginViewController.h"


@import MediaPlayer;

@interface AppDelegate ()



@end

@implementation AppDelegate

+ (MUPRestRequestOperationManager *)restDaoManager{
    static MUPRestRequestOperationManager * smcRestDaoManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        smcRestDaoManager = [MUPRestRequestOperationManager new];
        smcRestDaoManager.operationQueue.maxConcurrentOperationCount = 6;
        smcRestDaoManager.errorHandler = ^NSError * (MUPHTTPRequestOperation * operation, NSError * error){
            return smc_parseOperationError(operation, error);
        };
    });
    return smcRestDaoManager;
}

+ (AppDelegate *)sharecInstance{
    return [[UIApplication sharedApplication] delegate];
}

+ (NSString *)uid{
    UInt64 uid =  [AppDelegate sharecInstance].user.user.uid;
    return [NSString stringWithFormat:@"%@", @(uid)];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//    [UCEnvironment setServiceEnvironment:UCServiceEnvironmentDevelop];

    [self networkStatusObserve];
    [MobClick startWithAppkey:@"555e934a67e58ee3cd008061" reportPolicy:BATCH channelId:nil];
    NSString * version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"hortVersionString"];
    [MobClick setAppVersion:version];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UITabBarController * rootVC = [[self class] initUI];
    self.window.rootViewController = rootVC;
    [self setWindow:_window];
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    if (!_isLogined) {
        [self presentLoginViewController];
    }
}

- (void)presentLoginViewController{
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

+ (UINavigationController *) initUI{

//    [UCEnvironment setServiceEnvironment:UCServiceEnvironmentProduction];
//    UITabBarController * tabCtrl = [[UITabBarController alloc] init];
//    [[UITabBar appearance] setTintColor:[UIColor colorWithRed:0.86 green:0.28 blue:0.5 alpha:1]];
    
    
//    [tabCtrl setViewControllers:@[[FWLoginViewController new] ] animated:YES];
    
//    [tabCtrl setSelectedViewController: tabCtrl.viewControllers[0]];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[FWLoginViewController new]];
    
    return navi;
}


+ (UIViewController *)topMostViewController{
    UIViewController * topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

- (void)addIapPaymentObserve {
}
- (void)remvoeIapPaymentObserve {
}
- (void)networkStatusObserve {
    [[MUPNetworkReachabilityManager sharedManager] startMonitoring];
    
    [self.notificationController observeNotification:MUPNetworkingReachabilityDidChangeNotification object:nil block:^(NSNotification *notification) {
        NSNumber *statusNumber = notification.userInfo[MUPNetworkingReachabilityNotificationStatusItem];
        MUPNetworkReachabilityStatus status = statusNumber.integerValue;
        switch (status) {
            case MUPNetworkReachabilityStatusNotReachable: {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前没有连接到任何网络，请网络恢复后重新登录再进行使用" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
                [alert show];
                MUPLogDebug(@"reachabilityChanged:kNotReachable");
                //                _isLogined = false;
                //                [self presentLoginViewController];
                break;
            }
            case MUPNetworkReachabilityStatusReachableViaWiFi:
                MUPLogDebug(@"reachabilityChanged:ReachableViaWiFi");
                break;
            case MUPNetworkReachabilityStatusReachableViaWWAN:
                MUPLogDebug(@"reachabilityChanged:kReachableViaWWAN");
                break;
            default:{
                NSLog(@"Unkown network status");
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前没有连接到任何网络，请网络恢复后重新登录后再进行使用" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
                [alert show];
                break;
            }
        }
    }];
}
@end
